<?php
namespace app\article\controller;
use think\Db;
use think\Request;
use app\common\controller\AdminBase;

class ApiAdminArticleModel extends AdminBase
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemTagsRel = 'ArticlesTagsRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
        $this->itemTagsRelModelNameSpace = 'app\article\model\ArticlesTagsRel';
    }
    
    public function itemAdd()
    {
        $title = input('post.title');
        $name = input('post.name');
        $itemInfo = db('ArticlesModel')->where('title',$title)->find();
        if ($itemInfo) {
            return jsonError('标题已存在');
        }
        db('ArticlesModel')->insert(array('title' => $title,'name' => $name));
        $articlesModelInfo = db('ArticlesModel')->where('title',$title)->find();
        return jsonSuccess('操作成功');
    }
    
    public function itemEdit()
    {
        $id = input('post.id');
        $title = input('post.title');
        $itemInfo = db('ArticlesModel')->where('id',$id)->find();
        db('ArticlesModel')->where('id',$id)->update(array('title' => $title));
        return jsonSuccess('操作成功');
    }
	
    public function itemDel()
    {
        $id = input('post.id');
        if (!$id) {
          return jsonError('缺少参数');
        }
        $itemInfo = db('ArticlesModel')->where('id',$id)->find();
        if (!$itemInfo) {
            return jsonError('删除项不存在');
        }
        db('ArticlesModel')->where('id',$id)->delete();
        return jsonSuccess('操作成功');
    }

    public function itemInfo()
    {
        $id = input('post.id');
        $modelName = input('post.modelName');
        if (!$id && !$modelName) {
          return jsonError('缺少参数');
        }
        if ($id) {
        	$itemInfo = db('ArticlesModel')->where('id',$id)->find();
        }
        if ($modelName) {
        	$itemInfo = db('ArticlesModel')->where('name',$modelName)->find();
        }
	    return jsonSuccess('',$itemInfo);
    }
    
    public function itemList()
    {
        $itemList = db('ArticlesModel')->order('id','asc')->select();
        return jsonSuccess('',$itemList);
    }
    
}